package com.yupi.springbootinit.manager;/*
@author name
@data 2025/3/25-10:12
*/

import cn.hutool.core.collection.CollUtil;
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionChoice;
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionRequest;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessage;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessageRole;
import com.volcengine.ark.runtime.service.ArkService;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class BigModelHuoShan {
    @Resource
    private ArkService arkService;

    /**
     * 封装用户，ai输出的历史记录
     * @param systemPrompt
     * @param userPrompt
     * @return
     */
    public String doChat(String systemPrompt, String userPrompt){

        final List<ChatMessage> messages = new ArrayList<>();
        final ChatMessage SystemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content(systemPrompt).build();
        final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER).content(userPrompt).build();

        messages.add(SystemMessage);
        messages.add(userMessage);

        return doChat(messages);
    }

    /**
     * 调用AI 聊天
     * @param messages
     * @return
     */
    public String doChat(List<ChatMessage> messages){

        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                // 指定您创建的方舟推理接入点 ID，此处已帮您修改为您的推理接入点 ID
                .model("ep-20250325102550-dhjjr")
                .messages(messages)
                .build();

        List<ChatCompletionChoice> choiceList = arkService.createChatCompletion(chatCompletionRequest).getChoices();
        if(CollUtil.isEmpty(choiceList)){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        String content = (String)choiceList.get(0).getMessage().getContent();
        log.info("大模型输出：{}",content);

        arkService.shutdownExecutor();

        return content;
    }
}
